<?php
/*
 * Created on Oct 9, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<script type="text/javascript">
$(function() {
	$("a.ajax-edit").click(function(){
		$('#dialog').load($(this).attr('href'));
		$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 325,
			width: 450,
			title: 'Edit User',
			modal: true,
			buttons: {
				'Save': function() {
					// Validation
					jQuery("#userForm").validate({
						submitHandler: function(form) {
							$(form).ajaxForm({
								success: function(data){
									if(data.status=="success"){
										$("#user_loginname"+data.id).html(data.loginname);
										$("#user_fullname"+data.id).html(data.first_name + ' ' + data.last_name);
										$("#user_email"+data.id).html(data.email);
										//alert("You have just updated a record.");
										$("#dialog").dialog('close');
									}
									else{
										alert(data.messages);
									}
								},
								dataType: 'json'});
							return false;
						}
					});
		            $('#userForm').submit();
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				$('#userForm input').val('').removeClass('ui-state-error');
			}
		});
		$("#dialog").dialog("open");
		return false;
	});

	$("a.ajax-delete").click(function(){
		var delLink = $(this).attr("href");
		var temp = new Array();
		temp = delLink.split('/');
		id = temp[temp.length-1];
		$("#dialog").html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>This item will be permanently deleted and cannot be recovered. Are you sure?</p>')
		$("#dialog").dialog({
			title: 'Delete "' + $(this).attr('title') + '"',
			autoOpen: true,
			height: 180,
			buttons: {
				'Delete': function() {
					// Submit delete item
					$.get(delLink, function(){
						alert("You have just removed a record.");
					}, "json");
					$("#"+id).remove();
					$(this).dialog('close');
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			}
		});
		$("#dialog").dialog("open");
		return false;
	});

	$('#addUser').click(function() {
		$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 300,
			width: 450,
			title: 'Create User',
			modal: true,
			buttons: {
				'Create': function() {
					// Validation
					jQuery("#userForm").validate({
						submitHandler: function(form) {
							$(form).ajaxForm({
								success: function(data){
									if(data.status=="success"){
										var $table = $("#userBrowse tbody");
										var $userId = data.id;
										$.ajax({
										  url: "<?=site_url(array('c' => 'cms_user', 'm' => 'find_role', 'user_id' => $userId))?>",
										  cache: false,
										  success: function(html){
										    $table.append(html);
										  }
										});
										$("#dialog").dialog('close');
									}
									else{
										alert(data.messages);
									}
								},
								dataType: 'json'
							});
						}
					});
		            $('#userForm').submit();
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				$('#userForm input').val('').removeClass('ui-state-error');
			}
		});
		$('#dialog').load($(this).attr('href'));
		$("#dialog").dialog("open");
		return false;
	});
});
</script>
<div style="text-align: right;">
	<a id="addUser" href="<?=site_url(array("c" => "cms_user", "m" => "add_user"))?>">
		<img src="<?=image_url();?>function/add.gif" width="106" height="31" border="0">
	</a>
</div>
<table id="userBrowse" class="cont-table-list" cellpadding="5" cellspacing="0" style="width: 100%;">
	<thead>
		<tr>
			<th class="small">Edit</th>
			<th class="small">Delete</th>
			<th>Login Name</th>
			<th>Full Name</th>
			<th>Email</th>
		</tr>
	</thead>
	<tbody>
		<?php if(is_array($users)): $i = 0; $per_page = $this->pagination->cur_page * $this->pagination->per_page; ?>
		<?php foreach($users as $item): $i++?>
		<tr class="<?= $i%2 ? 'odd' : 'even'?>" id="<?=$item['id']?>">
			<td class="small"><a class="ajax-edit" href="<?=site_url(array("c" => "cms_user", "m" => "edit_user", "id" => $item['id']))?>" alt="Edit"><img src="<?=image_url()?>icons/edit.gif" alt="Edit"/></a></td>
			<td class="small"><a class="ajax-delete" href="<?=site_url(array("c" => "cms_user", "m" => "delete_user", "id" => $item['id']))?>" title="<?=$group['name']?>" alt="Delete"><img src="<?=image_url()?>icons/del.gif" alt="Delete"/></a></td>
			<td id="user_loginname<?=$item['id']?>"><?=$item['loginname']?></td>
			<td id="user_fullname<?=$item['id']?>"><?=$item['first_name'] . " " . $item['last_name']?></td>
			<td id="user_email<?=$item['id']?>"><?=$item['email']?></td>
		</tr>
		<?php endforeach;?>
		<?php endif;?>
	</tbody>
</table>
<div id="temp"></div>
<div class="pagination">
<span>Result: <?=$this->pagination->total_rows?> items</span>
<?= $this->pagination->create_links();?>
</div>

